import { createApp } from "vue";
import piniaPersist from "pinia-plugin-persist";
import { createPinia } from "pinia";
import "./styles/index.scss";
// import "element-plus/dist/index.css";
import "./permission";
import App from "./App.vue";
import router from "./router";
import SvgIcon from "@renderer/components/SvgIcon/index.vue";
import SvgChoose from "@renderer/components/SvgChoose/index.vue";
import { errorHandler } from "./error";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
// 本地SVG图标
import "virtual:svg-icons-register";

import "@imengyu/vue3-context-menu/lib/vue3-context-menu.css";
import "./styles/variable.css";
import "jsoneditor";
import "animate.css";
import ElementPlus from "element-plus";
import "dayjs/locale/zh-cn";
import locale from "element-plus/es/locale/lang/zh-cn";

const app = createApp(App);
const store = createPinia();
store.use(piniaPersist);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

// 导入包含所有组件的文件夹
const requireComponents = [
  (import.meta as any).glob("./components/module/*.{vue,js,ts,jsx,tsx}"),
  (import.meta as any).glob("./components/edit/*.{vue,js,ts,jsx,tsx}"),
];
// console.log("requireComponents", requireComponents);

requireComponents.forEach(async (requireComponent) => {
  const files = Object.keys(await requireComponent);
  // console.log(files);
  files.forEach(async (fileName) => {
    // console.log('fileName', fileName.replace(/^.+\/([^/]+)\.vue$/, '$1'))
    const componentConfig = await requireComponent[fileName]();

    // 将文件名转换为PascalCase命名规则并去除前面的文件夹名字
    // const componentName = fileName.replace('./', '').split('/')[2].split('.')[0]
    const componentName = fileName.replace(/^.+\/([^/]+)\.vue$/, "$1");
    // console.log(name)
    // 全局注册组件
    app.component(componentName, componentConfig.default || componentConfig);
  });
});
app.component("SvgIcon", SvgIcon);
app.component("SvgChoose", SvgChoose);
app.use(router);
app.use(store);
app.use(ElementPlus, { locale });
errorHandler(app);

app.mount("#app");
